$.texyla("setDefaults", {
	imageUploadPath: "%texyla_base%/plugins/img-upload/img-upload.php",
	imageUploadWindowDimensions: [350, 200]
});

$.texyla("initPlugin", function () {
	this.options.imageUploadPath = this.expand(this.options.imageUploadPath);
});

$.texyla("extend", {
	imageUploadId: 0
});

$.texyla("addButton", "img_upload", function (e) {
	// kontrola načtení ajaxFileUpload
	if (typeof($.ajaxFileUpload) != "function") {
		alert("Error: jQuery plugin 'ajaxFileUpload' is not loaded");
		return;
	}

	var _this = this;
	var id_img = ++this.imageUploadId;
	var el = $(
		'<div>'
		+ this.lng.imgForUpload + '<br>'
		+ '<input type="file" name="fileToUpload' + id_img + '" id="fileToUpload' + id_img + '">'
		+ '<input type="hidden" name="action" value="wp_handle_sideload">' //add by krteczek for WordPress plugin
		+ '</div>'
	);
	
	this.window({
		name: "imageUpload",
		el: el,
		title: this.lng.imgUploadHeading,
		stayOpened: true,
		func: function () {
			//starting setting some animation when the ajax starts and completes
			el.ajaxStart(function () {
				el.html('<p class="wait">' + _this.lng.wait + '</p>');
				el.parent().parent().find(".ui-dialog-buttonpane").hide();
			}).ajaxComplete(function () {
				el.dialog("close");
			});
			
			/*
				prepareing ajax file upload
				url: the url of script file handling the uploaded files
							fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
				dataType: it support json, xml
				secureuri:use secure protocol
				success: call back function when the ajax complete
				error: callback function when the ajax failed
				
					*/
			$.ajaxFileUpload({
				url: _this.options.imageUploadPath + "?fileToUpload=fileToUpload" + id_img, 
				//secureuri: true,
				fileElementId: 'fileToUpload' + id_img,
				dataType: 'json',
				success: function (data, status) {
					if (data.error != '') {
						alert(data.error);
					} else {
						_this.buttons.img.call(_this);
						var winEl = _this.getWindow("img");
						winEl.find(".src").val(data.filename);
						winEl.find(".alt").focus();
						
					}
				},
				error: function (data, status, e) {
					alert(e);
				}
			});
			
			return false;
		}
	});
});